package com.fau.mm.action.index;

import java.io.BufferedOutputStream;
import java.io.OutputStream;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.poi.ss.usermodel.Workbook;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.context.annotation.Scope;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Controller;

import com.fau.mm.action.PublicAction;
import com.fau.mm.entity.MedicalAdmin;
import com.fau.mm.entity.MedicalMediaInfoCosts;
import com.fau.mm.util.DateUtil;
import com.fau.mm.util.FileNameEncoderUtil;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ModelDriven;

/**
 * @desc 媒体进线成本信息管理
 * @author dangjh
 * @datetime 2014年11月6日,上午10:43:52
 */
@Controller
@Scope("prototype")
@ParentPackage("default")
@Namespace("/")
public class MediaCostsAction extends PublicAction implements ModelDriven<MedicalMediaInfoCosts> {

	private static final long serialVersionUID = -604751067777620012L;

	private MedicalMediaInfoCosts cost = new MedicalMediaInfoCosts();
	
	/**
	 * @desc 实例化媒体进线成本信息列表页
	 * @return
	 * @author dangjh
	 * @datetime 2014年11月4日,下午1:22:19
	 */
	@Action(value = "init_media_costs_list", results = {@Result(location = "init_media_costs_list.jsp")})
	public String initMediaCostList(){
		return SUCCESS;
	}
	
	/**
	 * @desc 加载实例化媒体进线成本信息数据
	 * @return
	 * @author dangjh
	 * @datetime 2014年11月4日,下午1:24:07
	 */
	@Action(value = "media_costs_views")
	public String mediaCostViews(){
		this.map = ActionContext.getContext().getParameters();
		MedicalAdmin admin = (MedicalAdmin) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
		this.pageBean = this.baseAllService.getMediaService().findAllMediaInfoCost(map, cost, admin);
		this.map.clear();
		map.put("total", this.pageBean.getTotalRow());
		map.put("data", this.pageBean.getResult());
		HttpServletResponse response = ServletActionContext.getResponse();
		formatJSON(response, this.map);
		return null;
	}
	
	/**
	 * @desc 导出当前页面数据
	 * @return
	 * @author dangjh
	 * @datetime 2014年10月20日,上午11:51:07
	 */
	@Action(value = "export_current_media_costs_data")
	public String exportCurrent(){
		HttpServletResponse response = ServletActionContext.getResponse();
		HttpServletRequest request = ServletActionContext.getRequest();
		String ids = request.getParameter("ids");
		try {
			response.addHeader("Content-Disposition", "attachment;filename=" + FileNameEncoderUtil.encode(request,"媒体进线成本信息"+DateUtil.getDate()+".xls"));
			response.setContentType("application/vnd.ms-excel");
			OutputStream toClient = new BufferedOutputStream(response.getOutputStream());
			Workbook workbook = this.baseAllService.getMediaService().currentPageCost(ids);
			workbook.write(toClient);
			toClient.flush();
			toClient.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	
	/**
	 * @desc 导出所有媒体投放信息
	 * @return
	 * @author dangjh
	 * @datetime 2014年10月20日,上午11:51:49
	 */
	@Action(value = "export_all_media_costs_data")
	public String exportAll(){
		HttpServletResponse response = ServletActionContext.getResponse();
		HttpServletRequest request = ServletActionContext.getRequest();
		try {
			MedicalAdmin user = (MedicalAdmin) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
			response.addHeader("Content-Disposition", "attachment;filename=" + FileNameEncoderUtil.encode(request,"媒体进线成本信息"+DateUtil.getDate()+".xls"));
			response.setContentType("application/vnd.ms-excel");
			OutputStream toClient = new BufferedOutputStream(response.getOutputStream());
			Workbook workbook = this.baseAllService.getMediaService().allPageCost(user);
			workbook.write(toClient);
			toClient.flush();
			toClient.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	
	@Override
	public MedicalMediaInfoCosts getModel() {
		return cost;
	}

}
